const app = getApp()
const util = require('../../../utils/util.js')
var xrRequest = require("../../../vendor/hope-sdk/core/request");
var xrSdk = require('../../../vendor/hope-sdk/index.js');
var config = require('../../../config');
Page({
  data: {
    searchKey: "", //搜索关键词
    width: 200, //header宽度
    height: 64, //header高度
    inputTop: 0, //搜索框距离顶部距离
    arrowTop: 0, //箭头距离顶部距离
    dropScreenH: 0, //下拉筛选框距顶部距离
    scrollTop: 0,
    tabIndex: 0, //顶部筛选索引
    selectH: 0,
    
    isFirst: true,
    pullUpOn: true,
    pageIndex: 1,
    loadding: false,
    noContent: false,
    productList: [],
  },
  onLoad: function (options) {
    let obj = wx.getMenuButtonBoundingClientRect();
    this.setData({
      width: obj.left,
      height: obj.top + obj.height + 8,
      inputTop: obj.top + (obj.height - 30) / 2,
      arrowTop: obj.top + (obj.height - 32) / 2,
      searchKey: options.searchKey || ""
    }, () => {
      wx.getSystemInfo({
        success: (res) => {
          this.setData({
            //略小，避免误差带来的影响
            dropScreenH: this.data.height * 750 / res.windowWidth, // + 186,
            drawerH: res.windowHeight - res.windowWidth / 750 * 100 - this.data.height
          })
        }
      })
    });
  },
  onPullDownRefresh: function () {
    this.initData();
    this.initList();
  },
  initData: function () {
    this.setData({
      productList: [],
      pageIndex: 1,
      pullUpOn: true,
      loadding: true,
      noContent: false
    })
  },
  initList: function () {
    var that = this;
    var params = that.getParams();
    xrRequest.request(app,{
      url: config.service.myShopList,
      login: true,
      data: params,
      success: function (res) {
        that.setData({
          productList: that.data.productList.concat(res.data.data.data),
          pageIndex: that.data.pageIndex + 1,
          loadding: false
        })
        // 没有更多
        if (res.data.data.has_more == false &&
          that.data.productList.length > 0) {
          that.setData({
            pullUpOn: false
          })
        }
        // 空数据
        that.setData({
          noContent: that.data.productList.length == 0
        });
        wx.stopPullDownRefresh();
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    });
  },
  getParams() {
    var that = this;
    var params = {
      page: that.data.pageIndex,
      page_size: 10,
      keyword: that.data.searchKey,
      status: 1
    };
    // if (that.data.attrArr[0].isActive) {
    //   params.is_new = 1;
    // }
    // if (that.data.attrArr[1].isActive) {
    //   params.is_hot = 1;
    // }
    // if (that.data.attrArr[2].isActive) {
    //   params.is_recommend = 1;
    // }
    // if (!util.isEmpty(that.data.order)) {
    //   params.order = that.data.order;
    // } else if (that.data.dropdownList[0].selected) {
    //   params.order = "default";
    // } else if (that.data.dropdownList[1].selected) {
    //   params.order = "price_asc";
    // } else if (that.data.dropdownList[2].selected) {
    //   params.order = "price_desc";
    // }
    return params;
  },
  onReachBottom: function () {
    if (!this.data.pullUpOn) return;
    this.setData({
      loadding: true
    }, () => {
      this.initList();
    })
  },
  back: function () {
    if (this.data.drawer) {
      this.closeDrawer()
    } else {
      wx.navigateBack()
    }
  },
  search: function () {
    xrSdk.cookie.remove('searchKey');
    wx.navigateTo({
      url: '../../search/search'
    })
  },
  detail: function (e) {
    console.log(e);
    wx.navigateTo({
      url: '../info/info?id=' + e.currentTarget.dataset.id
    })
  },
  contactUs:function(e){
    var mobile = e.currentTarget.dataset.mobile;
    wx.makePhoneCall({
      phoneNumber: mobile
    })
  },
  onShow: function () {
    var that = this;
    let searchKey = xrSdk.cookie.get('searchKey') || '';
    wx.hideTabBar({
      success: function () {
        app.editTabbar();
      }
    });
    if (searchKey != this.data.searchKey || that.data.isFirst) {
      this.setData({
        searchKey: searchKey
      });
      this.onPullDownRefresh();
    }
    if (that.data.isFirst) {
      this.setData({
        isFirst: false,
      });
    }
  },
  tabbarSwitch: function (e) {
    if (e.detail != undefined) {
      wx.switchTab({
        url: e.detail.pagePath
      })
    }
  }
})